home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
cd_mci.exe
/
CD.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-07
|
14KB
|
611 lines
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include <toolhelp.h>
#include <stdio.h>
#include <string.h>
#include "mci.h"
#include "cd.h"
#define TIME_NONE -999999
#define INI_NAME "cdplay.ini"
#define INI_TOPIC_NAME "Names"
#define INI_TOPIC_SETTINGS "Settings"
#define INI_POSITION "Position"
#define MAX_CDNAME 64
HINSTANCE hInst;
char szAppName[] = "CD Player";
BOOL CALLBACK CdDlgProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam);
BOOL CALLBACK TitleDlgProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam);
enum CDMode { ModeNotReady, ModePause, ModePlaying, ModeStop, ModeScanning };
class CDPlayer : public CDAudio {
HWND hwnd;
int nTracks;
DWORD *PosTracks; // Positionen der einzelnen Tracks in Millisekunden
DWORD *LenTracks; // LΣngen der einzelnen Tracks in Millisekunden
int Track; // aktueller Track
DWORD Sync; // Systemzeit, bei dem das nΣchste "Sync" stattfindet
DWORD StartTime; // Systemzeit, bei dem die CD gestartet wurde
CDMode mode; // current status of drive
DWORD dwPosition; // always contains the disc position in ms
WORD wTimer; // ID of timer used to update display
char CDTitle[MAX_CDNAME]; // Titel der CD
char CDID[10]; // ID-String der CD
BOOL bCountUp; // count seconds up or down
protected:
DWORD Time();
int Synchronize(DWORD time = 0);
void DisplayTime(LONG time, WORD wID);
void UpdateDisplay(DWORD wTime);
void CheckReady();
void SetTrack(int wTrack);
void SetPosition(DWORD dwPos);
public:
int Timer();
int Login(); // Betimmt LΣnge der Tracks etc.
int Logout(); // Setzt die Parameter zurⁿck
int Resync();
int Forward();
int Back();
int Pause();
int Play();
int Stop();
void ToggleUp();
void EditTitle();
CDPlayer(HWND aWindow);
~CDPlayer();
};
char *String(UINT id)
{
static char szString[128];
if (LoadString(hInst, id, szString, sizeof szString))
return szString;
else
return "";
}
DWORD MciError(HWND hwnd, DWORD d)
{
char s[256];
if (d) {
mciGetErrorString(d, s, sizeof s);
MessageBox(hwnd, s, szAppName, MB_OK);
}
return d;
}
CDPlayer::CDPlayer(HWND aWindow) : CDAudio(aWindow)
{
nTracks = 0;
LenTracks = NULL;
mode = ModeNotReady;
Track = 0;
hwnd = aWindow;
Sync = 0;
dwPosition = 0;
bCountUp = TRUE;
wTimer = SetTimer(hwnd, 1, 1000, NULL);
}
CDPlayer::~CDPlayer()
{
if (wTimer)
KillTimer(hwnd, 1);
}
DWORD CDPlayer::Time()
{
return GetTickCount();
/*
TIMERINFO ti;
ti.dwSize = sizeof ti;
TimerCount(&ti);
return ti.dwmsSinceStart;
*/
}
int CDPlayer::Synchronize(DWORD time)
{
int t;
BOOL bPlay;
if (time == 0)
time = Time();
Sync = time + 5000;
SetTimeFormat(MCI_FORMAT_MILLISECONDS);
bPlay = Mode() == MCI_MODE_PLAY;
if (mode == ModeNotReady) {
dwPosition = 0;
Track = 0;
}
if (mode == ModePlaying && !bPlay) {
// CD has stopped
dwPosition = PosTracks[1];
Track = 0;
mode = ModeStop;
}
if (bPlay)
mode = ModePlaying;
if (mode == ModePlaying) {
dwPosition = Position();
Track = CurrentTrack();
StartTime = time - dwPosition;
if (Sync > StartTime + PosTracks[Track] + LenTracks[Track])
Sync = StartTime + PosTracks[Track] + LenTracks[Track];
}
return TRUE;
}
void CDPlayer::DisplayTime(LONG time, WORD wID)
{
char buff[20];
char buff2[20];
if (time == TIME_NONE) {
SetDlgItemText(hwnd, wID, " --:--");
return;
}
if (time >= 0) {
time /= 1000;
wsprintf(buff, " %02d:%02d", int(time / 60), int(time % 60));
} else {
time = -(time / 1000) + 1;
wsprintf(buff, "-%02d:%02d", int(time / 60), int(time % 60));
}
GetDlgItemText(hwnd, wID, buff2, sizeof buff2);
if (strcmp(buff, buff2))
SetDlgItemText(hwnd, wID, buff);
}
void CDPlayer::CheckReady()
{
BOOL bReady = Ready();
if (bReady && mode == ModeNotReady) {
// CD-Laufwerk ist gerade Ready geworden
HCURSOR hOldCur = SetCursor(LoadCursor(NULL, IDC_WAIT));
mode = ModeScanning;
UpdateDisplay(dwPosition);
Login();
SetCursor(hOldCur);
mode = ModeStop;
Sync = 0;
}
if (!bReady && mode != ModeNotReady ) {
// Laufwerk wurde gerade "gest÷rt"
Logout();
mode = ModeNotReady;
Sync = 0;
}
}
int CDPlayer::Resync()
{
CheckReady();
Synchronize();
UpdateDisplay(dwPosition);
if (wTimer)
KillTimer(hwnd, 1);
wTimer = SetTimer(hwnd, 1, 1000, NULL);
return TRUE;
}
void CDPlayer::UpdateDisplay(DWORD dwTime)
{
static char buff[MAX_CDNAME];
char *s; // title string to display
int t; // track number to display
static UINT modes[] = {
IDS_NOTREADY,
IDS_PAUSED,
IDS_PLAYING,
IDS_STOPPED,
IDS_SCANNING
};
t = Track;
s = *CDTitle ? CDTitle : String(modes[mode]);
if (mode == ModeNotReady || mode == ModeScanning) {
t = 0;
s = String(modes[mode]);
DisplayTime(TIME_NONE, ID_SINGLETIME);
DisplayTime(TIME_NONE, ID_TOTALTIME);
} else if (Track == 0) {
t = nTracks;
DisplayTime(0, ID_SINGLETIME);
DisplayTime(PosTracks[nTracks+1], ID_TOTALTIME);
} else {
if (bCountUp) {
DisplayTime(dwTime - PosTracks[Track], ID_SINGLETIME);
DisplayTime(dwTime - PosTracks[1], ID_TOTALTIME);
} else {
DisplayTime(PosTracks[Track+1] - dwTime, ID_SINGLETIME);
DisplayTime(PosTracks[nTracks+1] - dwTime, ID_TOTALTIME);
}
}
if (GetDlgItemInt(hwnd, ID_TRACKCOUNT, NULL, FALSE) != t)
SetDlgItemInt(hwnd, ID_TRACKCOUNT, t, FALSE);
GetDlgItemText(hwnd, ID_TITLE, buff, sizeof(buff));
if (strcmp(s, buff))
SetDlgItemText(hwnd, ID_TITLE, s);
}
int CDPlayer::Timer()
{
CheckReady();
DWORD dwTime = Time();
if (dwTime >= Sync) {
// Synchronisation required
Synchronize(dwTime);
}
if (mode == ModePlaying)
dwPosition = dwTime - StartTime;
UpdateDisplay(dwPosition);
return TRUE;
}
void CDPlayer::SetTrack(int aTrack)
{
Track = aTrack;
SetPosition(PosTracks[Track]);
}
void CDPlayer::SetPosition(DWORD dwPos)
{
dwPosition = dwPos;
if (mode == ModePlaying) {
SetTimeFormat(MCI_FORMAT_MILLISECONDS);
MciError(hwnd, CDAudio::Play(MCI_FROM, dwPosition));
}
Resync();
}
int CDPlayer::Forward()
{
int w = Track + 1;
if (w > nTracks)
return TRUE;
SetTrack(w);
return TRUE;
}
int CDPlayer::Back()
{
int w = Track - 1;
if (w < 1)
return TRUE;
SetTrack(w);
return TRUE;
}
int CDPlayer::Pause()
{
if (mode == ModePlaying) {
SetTimeFormat(MCI_FORMAT_MILLISECONDS);
dwPosition = Position();
CDAudio::Stop();
}
mode = ModePause;
Resync();
return TRUE;
}
int CDPlayer::Play()
{
if (mode == ModePlaying) {
// if playing, play restarts the current song
dwPosition = PosTracks[Track];
}
SetTimeFormat(MCI_FORMAT_MILLISECONDS);
MciError(hwnd, CDAudio::Play(MCI_FROM, dwPosition));
Resync();
return TRUE;
}
int CDPlayer::Stop()
{
CDAudio::Stop();
mode = ModeStop;
SetTrack(0);
return TRUE;
}
int CDPlayer::Logout()
{
EnableWindow(GetDlgItem(hwnd, ID_EDIT), FALSE);
if (LenTracks) {
delete LenTracks;
delete PosTracks;
LenTracks = NULL;
}
nTracks = 0;
Track = 0;
*CDTitle = '\0';
return TRUE;
}
int CDPlayer::Login()
{
int i;
Logout();
if (!Ready())
return FALSE;
nTracks = NumberO